package com.ctsi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.common.util.Search;
import com.ctsi.core.common.entity.LoginEmpInfo;
import com.ctsi.core.common.exception.MarsException;
import com.ctsi.core.common.util.$;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.system.dto.EmpInfoReqDTO;
import com.ctsi.system.entity.Emp;
import com.ctsi.system.enums.SystemExceptionEnum;
import com.ctsi.system.enums.UserExceptionEnum;
import com.ctsi.system.mapper.EmpMapper;
import com.ctsi.system.service.IEmpExtOrgPosService;
import com.ctsi.system.service.IEmpPosService;
import com.ctsi.system.service.IEmpService;
import com.ctsi.system.vo.EmpInfoResVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author wang xiao xiang
 */
@Service
@AllArgsConstructor
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

	private final IEmpPosService empPosService;
	private final IEmpExtOrgPosService empExtOrgPosService;

	@Override
	public IPage<Emp> listPage(Search search) {
		LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
		return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
	}

	@Override
	public LoginEmpInfo getLoginEmpInfo(Long empId) {
		LoginEmpInfo loginEmpInfo = new LoginEmpInfo();
		// 查询员工信息
		Emp emp = this.getById(empId);
		if ($.isNotNull(emp)) {
			loginEmpInfo = $.copy(emp, LoginEmpInfo.class);
			if ($.isNull(loginEmpInfo)) {
				throw new MarsException(SystemExceptionEnum.EMP_INFO_EMPTY);
			}
			// 获取附属机构和职位信息
			List<Dict> empExtOrgPosDictList = empExtOrgPosService.getEmpExtOrgPosDictList(emp.getId(), false);
			loginEmpInfo.setExtOrgPos(empExtOrgPosDictList);

			//获取所属职位信息
			List<Dict> empEmpPosDictList = empPosService.getEmpPosDictList(emp.getId(), false);
			loginEmpInfo.setPositions(empEmpPosDictList);
		} else {
			loginEmpInfo.setExtOrgPos(CollectionUtil.newArrayList());
			loginEmpInfo.setPositions(CollectionUtil.newArrayList());
		}
		return loginEmpInfo;
	}

	@Override
	public EmpInfoResVO getSysEmpInfo(Long empId) {
		EmpInfoResVO empInfoRes = new EmpInfoResVO();
		// 查询员工信息
		Emp emp = this.getById(empId);
		if ($.isNotNull(emp)) {
			empInfoRes = $.copy(emp, EmpInfoResVO.class);
			if ($.isEmpty(empInfoRes)) {
				throw new MarsException(SystemExceptionEnum.EMP_INFO_EMPTY);
			}
			// 获取附属机构和职位信息
			List<Dict> empExtOrgPosDictList = empExtOrgPosService.getEmpExtOrgPosDictList(emp.getId(), Boolean.TRUE);
			empInfoRes.setExtOrgPos(empExtOrgPosDictList);

			//获取所属职位信息
			List<Dict> empEmpPosDictList = empPosService.getEmpPosDictList(emp.getId(), Boolean.TRUE);
			empInfoRes.setPositions(empEmpPosDictList);
		} else {
			empInfoRes.setExtOrgPos(CollectionUtil.newArrayList());
			empInfoRes.setPositions(CollectionUtil.newArrayList());
		}
		return empInfoRes;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean setEmpInfo(Long empId, EmpInfoReqDTO empInfoReq) {
		Emp emp = this.getById(empId);
		if ($.isEmpty(emp)) {
			emp = new Emp();
			emp.setId(empId);
		}
		// 使用empInfoReq给emp赋值
		BeanUtils.copyProperties(empInfoReq, emp);
		//　保存或更新员工信息
		this.saveOrUpdate(emp);
		// 保存员工职位信息
		List<Long> posIdList = empInfoReq.getPosIdList();
		empPosService.setEmpPos(empId, posIdList);
		return Boolean.TRUE;
	}

	@Override
	public Long getOrgId(Long userId) {
		if ($.isEmpty(userId)) {
			throw new MarsException(UserExceptionEnum.USER_NOT_EXIST);
		}
		EmpInfoResVO sysEmpInfo = this.getSysEmpInfo(userId);
		if ($.isEmpty(sysEmpInfo)) {
			throw new MarsException(SystemExceptionEnum.EMP_INFO_EMPTY);
		}
		return sysEmpInfo.getOrgId();
	}
}
